package com.leyou.auth.web;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.leyou.auth.config.OSSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
@EnableScheduling //开启定时任务
public class CleanImageTask {

    @Autowired
    private OSSProperties prop;
    @Autowired
    private OSS client;

    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {

        //System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
    }

    private void tesrt(){
        //
        System.out.println("开始删除");
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = prop.getEndpoint();
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        //String accessKeyId = prop.getAccessKeyId();
        //String accessKeySecret = prop.getAccessKeySecret();
        String bucketName = "leyou-bzy";

        // 创建OSSClient实例。
        //OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 删除文件。key等同于ObjectName，表示删除OSS文件时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        List<String> keys = new ArrayList<String>();






        DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();

        System.out.println("删除成功");
        // 关闭OSSClient。
        //client.shutdown();

    }
}
